
global root_dir = "`1'"

include "$root_dir/code/config/config.do"


cap noi log using ${log_dir}/load_exchange_rates.log, replace name(dat)

*Handle empty arguments
global arg1 = cond("`2'" == "___EMPTY___", "", "`2'")
global arg2 = cond("`3'" == "___EMPTY___", "", "`3'")
global arg3 = cond("`4'" == "___EMPTY___", "", "`4'")
global arg4 = cond("`5'" == "___EMPTY___", "", "`5'")

if "$arg1" != "" {
    global weight_category "$arg1"
    di "Weight category: ${weight_category}"
}

if "$arg2" != "" {
    global weight_versions "$arg2"
    di "Weight versions: ${weight_versions}"
}

if "$arg3" != "" {
    global weight_window "$arg3"
    di "Weight window: ${weight_window}"
}

if "$arg4" != "" {
	global wtype "$arg4"
}
di "${wtype}"
capture noi {


******************************************
* Import exchange rates from UNSTATS  *
******************************************
* Different exchange rates (IMF; AMA) and population | UNSTATS | https://unstats.un.org/unsd/snaama/downloads | downloaded August 9th 2019
import excel ${mow_data_raw}/GDP/UNSTATS_exchange_rate_and_population.xls, sheet("Download-XPop") cellrange(A3:AY663) firstrow clear
* Only keep exchange rate. 
keep if Measure =="IMF based exchange rate"
drop Measure CountryID Currency

*Reshape. 
rename E y1970
rename F y1971
rename G y1972
rename H y1973
rename I y1974
rename J y1975
rename K y1976
rename L y1977
rename M y1978
rename N y1979
rename O y1980
rename P y1981
rename Q y1982
rename R y1983
rename S y1984
rename T y1985
rename U y1986
rename V y1987
rename W y1988
rename X y1989
rename Y y1990
rename Z y1991
rename AA y1992
rename AB y1993
rename AC y1994
rename AD y1995
rename AE y1996
rename AF y1997
rename AG y1998
rename AH y1999
rename AI y2000
rename AJ y2001
rename AK y2002
rename AL y2003
rename AM y2004
rename AN y2005
rename AO y2006
rename AP y2007
rename AQ y2008
rename AR y2009
rename AS y2010
rename AT y2011
rename AU y2012
rename AV y2013
rename AW y2014
rename AX y2015
rename AY y2016


replace Country="Czechia" if Country=="Czech Republic" 

tempfile GDP_UNSTATS
save `GDP_UNSTATS'

*Import the concordance between country names and country codes.
*List of countries and corresponding ISO alpha 2 and ISO alpha 3 and iso numeric codes | by hand, based on https://www.iso.org/obp/ui/#iso:pub:PUB500001:en
import excel ${mow_data_raw}/countries/country_3_code.xlsx, sheet("Sheet1") cellrange(A1:C249) firstrow clear

*Change country names to match with country codes. 
rename COUNTRY Country
replace Country="Bolivia (Plurinational State of)" if Country=="Bolivia"
replace Country="Cabo Verde" if Country=="Cape Verde"
replace Country="Curaçao" if Country=="Curacao"
replace Country="Côte d'Ivoire" if Country=="Cote d'Ivoire"
replace Country="Czechia" if Country=="Czech Republic"
replace Country="D.R. of the Congo" if Country=="Democratic Republic of the Congo"
replace Country="China, Hong Kong SAR" if Country=="Hong Kong"
replace Country="Iran (Islamic Republic of)" if Country=="Iran, Islamic Republic of"
replace Country="Republic of Korea" if Country=="Korea, Republic of"
replace Country="D.P.R. of Korea" if Country=="Korea, Democratic People's Republic of"
replace Country="Lao People's DR" if Country=="Lao People's Democratic Republic"
replace Country="China, Macao SAR" if Country=="Macao"
replace Country="TFYR of Macedonia" if Country=="Macedonia, the Former Yugoslav Republic of"
replace Country="Micronesia (FS of)" if Country=="Microneasia, Federated States of"
replace Country="Republic of Moldova" if Country=="Moldova, Republic of"
replace Country="State of Palestine" if Country=="Palestine, State of"
replace Country="St. Vincent and the Grenadines" if Country=="Saint Vincent and the Grenadines"
replace Country="U.R. of Tanzania: Mainland" if Country=="United Republic of Tanzania"
replace Country="Venezuela (Bolivarian Republic of)" if Country=="Venezuela"

tempfile country_code
save `country_code'


*Merge it with the country code dataset.
merge 1:1 Country using `GDP_UNSTATS'

keep if _merge==3
drop _merge A2ISO Country

*Reshape.
reshape long y, i(A3UN) j(year)
rename A3UN country
rename y exchangerate
replace country=lower(country)
label variable exchangerate "Exchange rate"

tempfile UN_ER
save `UN_ER'

* We add the exchange rate for taiwanese currency from separate file.
* Average over the period | Central Bank of the Republic of China | https://www.cbc.gov.tw/en/cp-480-1879-66035-2.html
import excel ${mow_data_raw}/exchange_rates/Taiwan_exchange_rate.xls, sheet("Sheet1") firstrow clear
rename PeriodAverage year
rename NTDUSD exchangerate
gen country="twn"

*Merge data again with the exchange rates from the other countries. 
merge 1:1 country year using `UN_ER'
drop _merge

* Change name of countries so it matches with other data
replace country="usa-sic" if country=="usa"
replace country="uk" if country=="gbr"
replace country="ger" if country=="deu"


*Before we imported exchange rates from the world bank where the exchange rates
*where in local currencies before countries joined the EU (and after we set it 
*to the Euro exchange rate). Here in the UNSTATS exchange rate dataset we have
*euro for all countries who joined the EU for all time periods. Change that here
*(with the last exchange conversion rate before the switch to Euro), in order to 
*not change the code below again with the exchange rate. 

replace exchangerate =exchangerate*166.386 if country=="esp" & year<1999
replace exchangerate= exchangerate*13.7603 if country=="aut" & year<1999
replace exchangerate= exchangerate*40.3399 if country=="bel" & year<1999
replace exchangerate= exchangerate*5.94573 if country=="fin" & year<1999
replace exchangerate= exchangerate*6.55957 if country=="fra" & year<1999
replace exchangerate= exchangerate*1.95583 if country=="ger" & year<1999
replace exchangerate= exchangerate*340.750 if country=="grc" & year<2001
replace exchangerate= exchangerate*0.787564 if country=="irl" & year<1999
replace exchangerate= exchangerate*1936.27 if country=="ita" & year<1999
replace exchangerate= exchangerate*2.20371 if country=="nld" & year<1999
replace exchangerate= exchangerate*200.482 if country=="prt" & year<1999
replace exchangerate= exchangerate*40.3399 if country=="lux" & year<1999

replace exchangerate= exchangerate*239.64 if country=="svn" & year<2007
replace exchangerate= exchangerate*30.1260 if country=="svk" & year<2009
replace exchangerate= exchangerate*0.429300 if country=="mlt" & year<2008
replace exchangerate= exchangerate*0.585274 if country=="cyp" & year<2008
replace exchangerate= exchangerate*15.6466 if country=="est" & year<2011

replace exchangerate= exchangerate*3.4528 if country=="ltu" & year<2015
replace exchangerate= exchangerate*0.702804 if country=="lva" & year<2014


save ${mow_data_proc}/UN_ER.dta, replace

}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close dat